﻿import { Component, Injector, ViewChild} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { Tab } from './tab.model';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementGetEditComponent } from './requirement-get_edit.component'
import { RequirementsForModelComponentDto,StandardPartsFeatureDto, UsingFormServiceProxy,UsingFormState } from '@shared/service-proxies/service-proxies';
import { RequirementGetTabComponent } from './requirement-get_tab.component';

@Component({
    templateUrl: './requirement-get.component.html',
    styleUrls: ['./requirement-get.component.less'],
    animations: [appModuleAnimation()]
})
/**
 * 需求领用
 */
export class RequirementGetComponent extends AppComponentBase{
    @ViewChild('requirementGetEditComponent') requirementGetEditComponent: RequirementGetEditComponent;
    @ViewChild('RequirementGetTabComponent') requirementGeteTabComponent: RequirementGetTabComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    tabs: Array<Tab>;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;

    constructor(
        injector: Injector,
        private _usingFormServiceProxy: UsingFormServiceProxy) {
        super(injector);
        this.tabs = new Array<Tab>();
    }
    addTab(usingFormDto: RequirementsForModelComponentDto): void {
        if (this.tabs.length == 0) {
            let tab: Tab = new Tab("领用记录");
            tab.index = this.tabs.length;
            tab.record = usingFormDto;
            tab.active = true;
            this.tabs.push(tab);
        } else {
            this.tabs[0].active = true;
            this.tabs[0].name = "领用记录";
        }

        if (this.requirementGeteTabComponent) {
            this.requirementGeteTabComponent.setRequirementPackage(usingFormDto);
            this.requirementGeteTabComponent.getRequirementItems();
        }
    }
    getUsingFormList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._usingFormServiceProxy.getAllRequirementsForModelComponent(
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                if(result.items){
                    for(var key in result.items){
                        if(!result.items[key].features){
                            result.items[key].features = new StandardPartsFeatureDto();
                        }
                    }
                }
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();

            });

    }
    addSelect(_pRequirementsForModelComponentDto:RequirementsForModelComponentDto): void {
        this.requirementGetEditComponent.show(_pRequirementsForModelComponentDto);
    }
    getUsingFormStateStateString(state:number):string{
        return UsingFormState[state];
    }

    onResize(event): void {
    
    }

    ngOnInit(): void {
        const self = this;
    }
}
